package com.snail.commonlibrary;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;

import com.snail.util.PhoneUtil;

/**
 * 获取手机信息类 class description：读取Sim卡信息<BR>
 * PS： 必须在加入各种权限 <BR>
 * Date:2012-3-12<BR>
 * 
 * @version 1.00
 * @author CODYY)peijiangping
 */
public class PhoneInfo {
	/**
	 * TelephonyManager提供设备上获取通讯服务信息的入口。 应用程序可以使用这个类方法确定的电信服务商和国家 以及某些类型的用户访问信息。
	 * 应用程序也可以注册一个监听器到电话收状态的变化。不需要直接实例化这个类
	 * 使用Context.getSystemService(Context.TELEPHONY_SERVICE)来获取这个类的实例。
	 */
	private TelephonyManager telephonyManager;
	private PackageManager packageManager;
	private String packageName;

	public PhoneInfo(Context context) {
		telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		packageManager = context.getPackageManager();
		packageName = context.getPackageName();
	}

	/**
	 * Role:获取当前设置的电话号码 <BR>
	 * Date:2012-3-12 <BR>
	 * 
	 * @author CODYY)peijiangping
	 */
	public String getNativePhoneNumber() {
		String NativePhoneNumber = null;
		NativePhoneNumber = telephonyManager.getLine1Number();

		GsmCellLocation loc = (GsmCellLocation) telephonyManager.getCellLocation();

		int cellid = loc.getCid();
		int lac = loc.getLac();

		String deviceid = telephonyManager.getDeviceId();
		String phonenumber = telephonyManager.getLine1Number();
		String softwareversion = telephonyManager.getDeviceSoftwareVersion();
		String operatorname = telephonyManager.getNetworkOperatorName();
		String simcountrycode = telephonyManager.getSimCountryIso();
		String simoperator = telephonyManager.getSimOperatorName();
		String simserialno = telephonyManager.getSimSerialNumber();
		
		// String networktype =
		// getNetworkTypeString(telephonyManager.getNetworkType());
		// String phonetype =
		// getPhoneTypeString(telephonyManager.getPhoneType());

		Log.v("Applicataon", "CellID: " + cellid);
		Log.v("Applicataon", "LAC: " + lac);
		Log.v("Applicataon", "Device ID: " + deviceid);
		Log.v("Applicataon", "Phone Number: " + phonenumber);
		Log.v("Applicataon", "Software Version: " + softwareversion);
		Log.v("Applicataon", "Operator Name: " + operatorname);
		Log.v("Applicataon", "SIM Country Code: " + simcountrycode);
		Log.v("Applicataon", "SIM Operator: " + simoperator);
		Log.v("Applicataon", "SIM Serial No.: " + simserialno);
		return NativePhoneNumber;
	}

	/**
	 * 获取运营商与IMEI的拼结字符
	 * @return
	 */
	public String getPhoneIMEI() {
		String deviceid = ""; 
		if(null !=  PhoneUtil.getDeviceUUID())
		    deviceid = PhoneUtil.getDeviceUUID();
		return deviceid;
	}

	public String getVersionName(){
		String version = "0";
		try {
			PackageInfo packInfo = packageManager.getPackageInfo(packageName,0);
			 version = packInfo.versionName;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// getPackageName()是你当前类的包名，0代表是获取版本信息
		return version;
	}

	/**
	 * Role:Telecom service providers获取手机服务商信息 <BR>
	 * 需要加入权限<uses-permission
	 * android:name="android.permission.READ_PHONE_STATE"/> <BR>
	 * Date:2012-3-12 <BR>
	 * 
	 * @author CODYY)peijiangping
	 */
	public String getProvidersName() {
		String ProvidersName = "0";
		
		// 返回唯一的用户ID;就是这张卡的编号神马的
		String IMSI = telephonyManager.getSubscriberId();
		if(null != IMSI){
        		// IMSI号前面3位460是国家，紧接着后面2位00 02是中国移动，01是中国联通，03是中国电信。
        		if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
        			ProvidersName = "2";//"中国移动";
        		} else if (IMSI.startsWith("46001")) {
        			ProvidersName = "3";//"中国联通";
        		} else if (IMSI.startsWith("46003")) {
        			ProvidersName = "1";//"中国电信";
        		}
		}
		return ProvidersName;
	}
}